package com.rjpacket.supernews.app.myviews;

import android.content.Context;
import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * Created by aaa on 15-3-17.
 */
public class MyHeadView extends ImageView {
    private Bitmap bitmap;

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }

    public MyHeadView(Context context) {
        super(context);
    }

    public MyHeadView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        setScaleType(ScaleType.CENTER_CROP);
        Drawable drawable = getDrawable();

        if (null == drawable) {
            return;
        }

//        // 将drawable转换成bitmap==>网上找的
//        Bitmap bitmap = Bitmap
//                .createBitmap(
//                        drawable.getIntrinsicWidth(),
//                        drawable.getIntrinsicHeight(),
//                        drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
//                                : Bitmap.Config.RGB_565);
//
//        Canvas srcCanvas = new Canvas(bitmap);
//
//        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
//                drawable.getIntrinsicHeight());
//
//        drawable.draw(srcCanvas);

        float cx = getWidth() / 2;
        float cy = getHeight() / 2;

        float radius = Math.min(getWidth(), getHeight()) / 2;

        Paint borderPaint = new Paint();
        borderPaint.setAntiAlias(true);
        borderPaint.setColor(Color.WHITE);

        canvas.drawCircle(cx, cy, radius, borderPaint);

        // 画图
        BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
                Shader.TileMode.CLAMP);
        Paint paint = new Paint();
        paint.setShader(shader);
        paint.setAntiAlias(true);
        canvas.drawCircle(cx, cy, radius - 5, paint);
    }


}
